package com.xiaoshuidi.cloud.module.rooms.api;

import cn.hutool.core.bean.BeanUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.rooms.api.map.MapApi;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.GetMapRespApiVO;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.MapFindSearchReqApiVO;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.MetroRouteRespApiVO;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.MetroSiteRespApiVO;
import com.xiaoshuidi.cloud.module.rooms.controller.app.map.vo.GetMapRespVO;
import com.xiaoshuidi.cloud.module.rooms.controller.app.map.vo.MetroRouteRespVO;
import com.xiaoshuidi.cloud.module.rooms.controller.app.map.vo.MetroSiteRespVO;
import com.xiaoshuidi.cloud.module.rooms.service.MapService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.xiaoshuidi.cloud.module.system.enums.ApiConstants.VERSION;

@Slf4j
@RestController
@DubboService(version = VERSION)
public class MapApiImpl implements MapApi {
    @Resource
    private MapService mapService;

    /**
     * 根据经纬度获取范围内房源数据
     *
     * @param maxLongitude     最大经度
     * @param minLongitude     最小经度
     * @param maxLatitude      最大维度
     * @param minLatitude      最小经度
     * @param zoom             缩放等级
     * @param tenantId
     * @param isConcentrated
     * @param district         区域
     * @param monthRentalMin   租金范围-下限
     * @param monthRentalMax   租金范围-上限
     * @param longitude        地铁站经度
     * @param latitude         地铁站纬度
     * @param isWhole          出租方式,是否整租 true:整租 false:合租
     * @param bedRoomNum       户型-几室 1:一室 2:二室 3:三室 4:四室 5:其他
     * @param publicFacilities 特色需求(公共设施)
     * @param roomHouseState 房源状态 1：可租房源 2：全部房源
     * @return
     */
    @Override
    public CommonResult<List<GetMapRespApiVO>> mapFindRoom(Double maxLongitude, Double minLongitude, Double maxLatitude, Double minLatitude, Long tenantId, Boolean isConcentrated, String district, Double monthRentalMin, Double monthRentalMax, Double longitude, Double latitude, Boolean isWhole, Integer bedRoomNum, String publicFacilities, String roomHouseState, Double zoom) {
        log.info("[地图找房Api接口被调用 MapApi.mapFindRoom] 地图找房参数接收参数：maxLongitude:{},minLongitude:{},maxLatitude:{},minLatitude:{},zoom:{},tenantId:{},isConcentrated:{}" +
                        " district{},monthRentalMin:{},monthRentalMax:{},longitude:{},latitude:{},isWhole:{},bedRoomNum:{},publicFacilities:{},roomHouseState:{}",
                maxLongitude, minLongitude, maxLatitude, minLatitude, zoom, tenantId, isConcentrated, district, monthRentalMin, monthRentalMax, longitude, latitude, isWhole, bedRoomNum, publicFacilities, roomHouseState);
        List<GetMapRespVO> checkedData = mapService.getMap(maxLongitude, minLongitude, maxLatitude, minLatitude,
                zoom, tenantId, isConcentrated, district, monthRentalMin,
                monthRentalMax, longitude, latitude, isWhole, bedRoomNum, publicFacilities, roomHouseState).getCheckedData();
        List<GetMapRespApiVO> respVOS = new ArrayList<>();
        for (GetMapRespVO checkedDatum : checkedData) {
            GetMapRespApiVO getMapRespApiVO = new GetMapRespApiVO();
            BeanUtil.copyProperties(checkedDatum, getMapRespApiVO);
            respVOS.add(getMapRespApiVO);
        }
        return CommonResult.success(respVOS) ;
    }

    /**
     * 地图找房-搜索
     * @param reqVO
     * @return
     */
    @Override
    public CommonResult<PageResult<GetMapRespApiVO>> mapFindSearch(MapFindSearchReqApiVO reqVO) {
        log.info("[地图找房-搜索Api接口被调用 MapApi.mapFindSearch] 请求参数:{}", JsonUtils.toJsonString(reqVO));
        PageResult<GetMapRespVO> checkedData = mapService.mapFindSearch(reqVO.getSearchBox(), reqVO.getIsConcentrated(), reqVO.getRoomHouseState(), reqVO.getPageNo(), reqVO.getPageSize(), reqVO.getTenantId()).getCheckedData();
        List<GetMapRespVO> list = checkedData.getList();
        List<GetMapRespApiVO> respVOS = new ArrayList<>();
        for (GetMapRespVO getMapRespVO : list) {
            GetMapRespApiVO getMapRespApiVO = new GetMapRespApiVO();
            BeanUtil.copyProperties(getMapRespVO, getMapRespApiVO);
            respVOS.add(getMapRespApiVO);
        }
        return CommonResult.success(new PageResult<>(respVOS, checkedData.getTotal()));
    }

    /**
     * 获取所有地铁线路
     * @return
     */
    @Override
    public CommonResult<List<MetroRouteRespApiVO>> getMetroRoute() {
        log.info("[获取所有地铁线路Api接口被调用 MapApi.getMetroRoute]");
        List<MetroRouteRespVO> metroRoute = mapService.getMetroRoute();
        List<MetroRouteRespApiVO> respVOS = new ArrayList<>();
        for (MetroRouteRespVO metroRouteRespVO : metroRoute) {
            MetroRouteRespApiVO metroRouteRespApiVO = new MetroRouteRespApiVO();
            BeanUtil.copyProperties(metroRouteRespVO, metroRouteRespApiVO);
            respVOS.add(metroRouteRespApiVO);
        }
        return CommonResult.success(respVOS);
    }

    /**
     * 获取地铁线路下的站点
     * @param id
     * @return
     */
    @Override
    public CommonResult<List<MetroSiteRespApiVO>> getMetroSite(Long id) {
        log.info("[获取地铁线路下的站点Api接口被调用 MapApi.getMetroSite] 请求参数: id:{}", id);
        List<MetroSiteRespVO> metroSite = mapService.getMetroSite(id);
        List<MetroSiteRespApiVO> respVOS = new ArrayList<>();
        for (MetroSiteRespVO metroSiteRespVO : metroSite) {
            MetroSiteRespApiVO metroSiteRespApiVO = new MetroSiteRespApiVO();
            BeanUtil.copyProperties(metroSiteRespVO, metroSiteRespApiVO);
            respVOS.add(metroSiteRespApiVO);
        }
        return CommonResult.success(respVOS);
    }
}
